package com.tarena.lbs.service.impl;

import com.tarena.lbs.dao.MessageDao;
import com.tarena.lbs.dao.UserDao;
import com.tarena.lbs.entity.Merchant;
import com.tarena.lbs.entity.Message;
import com.tarena.lbs.entity.User;
import com.tarena.lbs.service.ApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class ApiServiceImpl implements ApiService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private MessageDao messageDao;

    @Override
    public User getUserById(Long id) {
        // 先从缓存获取
        String key = "lbs:user:" + id;
        User user = (User) redisTemplate.opsForValue().get(key);

        if (user == null) {
            // 缓存没有，从数据库获取
            user = userDao.getById(id);
            if (user != null) {
                // 放入缓存，设置过期时间
                redisTemplate.opsForValue().set(key, user, 3600, TimeUnit.SECONDS);
            }
        }
        return user;
    }

    @Override
    public List<Merchant> getNearbyMerchants(Double latitude, Double longitude, Double distance) {
        // 这里应该查询商家数据库获取附近商家
        // 为了示例，返回一个模拟数据
        List<Merchant> merchants = new ArrayList<>();

        Merchant merchant1 = new Merchant();
        merchant1.setId(1L);
        merchant1.setName("星巴克咖啡");
        merchant1.setAddress("北京市朝阳区建国路88号");
        merchant1.setLatitude(latitude + 0.01);
        merchant1.setLongitude(longitude + 0.01);
        merchant1.setPhone("010-12345678");
        merchant1.setBusinessHours("08:00-22:00");
        merchant1.setStatus(1);

        Merchant merchant2 = new Merchant();
        merchant2.setId(2L);
        merchant2.setName("肯德基");
        merchant2.setAddress("北京市朝阳区建国路89号");
        merchant2.setLatitude(latitude - 0.01);
        merchant2.setLongitude(longitude - 0.01);
        merchant2.setPhone("010-87654321");
        merchant2.setBusinessHours("07:00-23:00");
        merchant2.setStatus(1);

        merchants.add(merchant1);
        merchants.add(merchant2);

        return merchants;
    }

    @Override
    public boolean sendMessage(Long userId, String message) {
        Message msg = new Message();
        msg.setUserId(userId);
        msg.setMessage(message);
        msg.setCreateTime(new java.util.Date());
        int result = messageDao.insert(msg);
        return result > 0;
    }

//    @Override
//    public User registerUser(User user) {
//        // 注册用户逻辑
//        user.setCreateTime(new java.util.Date());
//        user.setUpdateTime(new java.util.Date());
//
//        int result = userDao.save(user);
//        if (result > 0) {
//            return user;
//        }
//        return null;
//    }
//
//    @Override
//    public User login(String username, String password) {
//        // 登录验证逻辑
//        User user = userDao.getByUsername(username);
//        if (user != null && user.getPassword().equals(password)) {
//            return user;
//        }
//        return null;
//    }
}